const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

async function testDatabase() {
  try {
    console.log('🔍 测试数据库连接...');
    
    // 测试数据库连接
    await prisma.$connect();
    console.log('✅ 数据库连接成功');
    
    // 检查数据库中的表
    console.log('\n📊 检查数据库表...');
    const tables = await prisma.$queryRaw`
      SELECT table_name 
      FROM information_schema.tables 
      WHERE table_schema = 'public'
    `;
    console.log('数据库中的表:', tables);
    
    // 检查 admin_user 表结构
    console.log('\n👥 检查 admin_user 表结构...');
    const columns = await prisma.$queryRaw`
      SELECT column_name, data_type, is_nullable, column_default
      FROM information_schema.columns 
      WHERE table_name = 'admin_user'
      ORDER BY ordinal_position
    `;
    console.log('admin_user 表结构:', columns);
    
    // 检查管理员账号数量
    console.log('\n👤 检查管理员账号...');
    const adminCount = await prisma.adminUser.count();
    console.log(`管理员账号数量: ${adminCount}`);
    
    if (adminCount > 0) {
      const admins = await prisma.adminUser.findMany({
        select: {
          id: true,
          username: true,
          email: true,
          isActive: true,
          createdAt: true
        }
      });
      console.log('管理员账号列表:', admins);
    }
    
    // 测试查询性能
    console.log('\n⚡ 测试查询性能...');
    const startTime = Date.now();
    await prisma.adminUser.findFirst();
    const endTime = Date.now();
    console.log(`查询耗时: ${endTime - startTime}ms`);
    
  } catch (error) {
    console.error('❌ 数据库测试失败:', error);
  } finally {
    await prisma.$disconnect();
    console.log('\n🔌 数据库连接已关闭');
  }
}

testDatabase(); 